<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 放置卡片区域 -->
      <el-card>
        <!-- 标签页组件 -->
        <el-tabs>
          <!-- 三个标签页 -->
          <el-tab-pane label="登录账户设置">
            <!-- 放置表单 -->
            <el-form
              ref="userForm"
              :model="userInfo"
              :rules="rules"
              label-width="120px"
              style="margin-left: 120px; margin-top:30px"
            >
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userInfo.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="password2">
                <el-input
                  v-model="userInfo.password2"
                  style="width:300px"
                  type="password"
                />
              </el-form-item>
              <el-form-item>
                <!-- 点击时调用 保存用户信息的接口 -->
                <el-button type="primary" @click="saveUser">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>

          <el-tab-pane label="个人详情">
            <!-- component占位符  :is 绑定一个变量 -->
            <component :is="userComponent" />
            <!-- <user-info /> -->
          </el-tab-pane>

          <el-tab-pane label="岗位详情">
            <!-- 放置岗位详情 -->
            <!-- <job-info /> -->
            <component :is="jobComponent" />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDetailById } from '@/api/user' // 获取个人信息的接口
import { saveUserDetailById } from '@/api/employees' // 保存个人信息的接口
import userInfo from './components/user-info' // 导入个人详情的页面
import JobInfo from './components/job-info' // 导入岗位信息的页面

export default {
  components: { userInfo, JobInfo },
  data() {
    return {
      userComponent: 'user-info', //
      jobComponent: 'job-info',
      userId: this.$route.params.id, // 这样子后面可以直接通过 this.userId 获取数据
      userInfo: {
        password2: '',
        username: ''
      },
      rules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        password2: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          {
            min: 6,
            max: 9,
            message: '密码的长度的6-9位',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getUserDetailById() // 调用获取用户信息的接口
  },
  mounted() {},
  methods: {
    async getUserDetailById() {
      this.userInfo = await getUserDetailById(this.userId)
    },
    async saveUser() {
      try {
        // 保存成功
        await this.$refs.userForm.validate()
        await saveUserDetailById({
          ...this.userInfo,
          password: this.userInfo.password2
        }) // 将新密码的值替换原密码的值
        this.$message.success('保存成功')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
